import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/landlord/appointment',
    name: 'LandlordAppointment',
    component: () => import('../views/landlord/AppointmentFeedback.vue'),
    meta: { requiresUser: true }
  },
  {
  path: '/landlord/maintenance',
  name: 'LandlordMaintenance',
  component: () => import('../views/landlord/MaintenanceFeedback.vue'),
  meta: { requiresUser: true }
  },
  {
  path: '/landlord/income',
  name: 'LandlordIncome',
  component: () => import('../views/landlord/RentalIncome.vue'),
  meta: { requiresUser: true }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/Admin.vue'),
    meta: { requiresAdmin: true },
    children: [
      {
        path: 'house',
        name: 'AdminHouse',
        component: () => import('../views/admin/HouseManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'user',
        name: 'AdminUser',
        component: () => import('../views/admin/UserManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'finance',
        name: 'AdminFinance',
        component: () => import('../views/admin/FinanceStatistics.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'feedback',
        name: 'AdminFeedback',
        component: () => import('../views/admin/FeedbackManagement.vue'),
        meta: { requiresAdmin: true }
      },
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue'),
    meta: { requiresUser: true }
  },
  {
    path: '/house',
    name: 'HouseList',
    component: () => import('../views/HouseListView.vue')
  },
  {
    path: '/house/:houseid',
    name: 'HouseDetails',
    component: () => import('../views/HouseDetailsView.vue')
  },
  {
    path: '/my-houses',
    component: () => import('../views/MyHouses.vue')
  },
  {
    path: '/chat',
    name: 'chat',
    component: () => import('../views/ChatView.vue')
  },
  {
    path: '/rent-application/:houseid',
    name: 'rentApplication',
    component: () => import('../views/RentApplication.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/aichat',
    name: 'AIChat',
    component: () => import('../views/AIChatRoom.vue'),
    meta: { requiresUser: true }
  },
  {
    path: '/messageList',
    name: 'MessageList',
    component: () => import('../views/MessageList.vue'),
    meta: { requiresUser: true }
  },
  {
    path: '/contract',
    name: 'ContractView',
    component: () => import('../views/ContractView.vue'),
    meta: { requiresAuth: true } 
  },
  {
    path: '/feedback',
    name: 'Feedback',
    component: () => import('../views/FeedbackView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const isAdmin = localStorage.getItem('isAdmin') === '1'
  
  // 不需要验证的路由
  if (to.path === '/login' || to.path === '/register') {
    if (token) {
      // 已登录用户不能访问登录和注册页
      next(isAdmin ? '/admin' : '/user')
    } else {
      next()
    }
    return
  }
  
  // 需要验证的路由
  if (!token) {
    next('/login')
    return
  }
  
  // 管理员路由权限验证
  if (to.meta.requiresAdmin && !isAdmin) {
    ElMessage.error('无权访问管理员页面')
    next('/user')
    return
  }
  
  // 普通用户路由权限验证
  if (to.meta.requiresUser && isAdmin) {
    ElMessage.error('请使用普通用户账号访问')
    next('/admin')
    return
  }
  
  next()
})

export default router